package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Collection;

public class CollectionDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Collection collection) {
		this.sql = "INSERT INTO Collections (description_collection, amout_books_collection) VALUES (?, ?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, collection.getDescriptionCollection());
			this.statement.setInt(2, collection.getAmountBooksCollection());
			this.statement.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void update(Collection collection) {
		open();
		this.sql = "UPDATE Collections SET description_collection = ?, amout_books_collection = ? WHERE id_collection = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, collection.getDescriptionCollection());
			this.statement.setInt(2, collection.getAmountBooksCollection());
			this.statement.setInt(3, collection.getIdCollection());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Collection collection) {
		open();
		this.sql = "DELETE FROM Collections WHERE id_collection = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, collection.getIdCollection());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Collection search(Integer id_collection) {
		open();
		this.sql = "SELECT * FROM Collections WHERE id_collection = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_collection);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Collection(result.getInt("id_collection"),
						result.getString("description_collection"),
						result.getInt("amout_books_collection"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Collection> list() {
		open();
		this.sql = "SELECT * FROM Collections";
		ArrayList<Collection> collections = new ArrayList<Collection>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				collections.add(new Collection(result.getInt("id_collection"),
						result.getString("description_colection"), result
								.getInt("amout_books_collection")));
			}
			return collections;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
